﻿using System.Collections.Generic;
using System.IO;
using iReaper.IndexService.Common;
using iReaper.IndexService.Common.TaskEngine;

namespace iReaper.IndexService.MSEvents
{
    public class LocalSyncupTask : TaskBase
    {

        public override void Start()
        {
            DirectoryInfo dir = new DirectoryInfo(Config.ContentFolder);
            if (!dir.Exists)
            {
                dir.Create();
            }
            else
            {
                var files = dir.GetFiles("*.xml");
                List<EventItem> buffer= new List<EventItem>();
                foreach (var f in files)
                {
                    int id;
                    string name = f.Name.Remove(f.Name.Length - ".xml".Length, ".xml".Length);
                    if (int.TryParse(name, out id))
                    {
                        EventItem item = new EventItem();
                        item.EventID = name;
                        buffer.Add(item);
                    }
                }

                SetResult(buffer.ToArray());
                Done();
            }
        }
    }
}
